package com.estate.struts.actions;

import java.io.PrintWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.estate.business.AllProjectService;
import com.estate.business.BillingService;
import com.estate.business.DeleProjectService;
import com.estate.business.ManageProjectService;
import com.estate.business.PriceProjectService;
import com.estate.business.UserInfoService;
import com.estate.utils.ProCatagoryUtil;
import com.estate.vo.Vo;
import com.estate.vo.impl.BillingVo;
import com.estate.vo.impl.DeleProjectVo;
import com.estate.vo.impl.ManageProjectVo;
import com.estate.vo.impl.PriceProjectVo;
import com.estate.vo.impl.UserInfoVo;

public class BillingAction extends EstateSupport implements
		ServletRequestAware, ServletResponseAware {

	private BillingService billingService;
	private PriceProjectService priceProjectService;
	private DeleProjectService deleProjectService;
	private ManageProjectService manageProjectService;
	private UserInfoService userInfoService;
	private AllProjectService allProjectService;

	private String method;

	private int id;
	private String proName;
	private String proCatagory;
	private String proId;
	private String billNum;
	private int billCatagory;
	private String billMoney;
	private UserInfoVo billUser;
	private Date billDate;
	private int lastRecordUserId;
	private String billUserId;

	private String multiType;
	private String multiDelete;
	private String delStr;
	private String deleteId;
	private int modifyId;
	private int deliverId;

	private HttpServletRequest request;
	private HttpServletResponse response;

	public String execute() throws Exception {
		System.out.println("in billing");
		System.out.println(method);
		String page = "";

		if ("list".equals(method)) {

			List<BillingVo> voList = billingService.findAllBilling();
			if (this.getMultiType() != null
					&& this.getMultiType().equals("true")) {
				this.getRequest().setAttribute("multiType", new Boolean(true));
			} else {
				this.getRequest().setAttribute("multiType", new Boolean(false));
			}
			for(int i=0;i<voList.size();i++){
				if(voList.get(i).getState()==1)
					voList.get(i).setStateName("未开票");
				else voList.get(i).setStateName("已开票");
			}
			this.getSession().setAttribute("billing", voList);
			page = "list";

		} else if ("delete".equals(method)) {
			if ("true".equals(multiDelete)) {
				System.out.println("!!!!!!!!!" + delStr);
				if (delStr != null && !"".equals(delStr) && !",".equals(delStr)) {
					String[] delArray = delStr.split(",");
					for (int i = 0; i < delArray.length; i++) {
						billingService
								.deleteByPK(Integer.parseInt(delArray[i]));
					}
				}
			} else {
				billingService.deleteByPK(Integer.parseInt(deleteId));
			}
			page = "refresh";
		} else if ("json".equals(method)) {
			System.out.println("~catagory"+this.getProCatagory());
			JSONArray proArr = getProArr(Integer
					.parseInt(this.getProCatagory()));
			
			response.setContentType("text/html;charset=UTF-8");
			System.out.println("3333");
			PrintWriter out = this.response.getWriter();
			System.out.println("2222");
			out.print(proArr);
			out.flush();
			out.close();
			page = null;
		} else if ("add".equals(method)) {
			HashMap<String, Integer> map = userInfoService.findAllNameAndId();
			ServletActionContext.getRequest().setAttribute("billUserSelect",
					map);
			page="add";
		} else if("insert".equals(method)){
			System.out.println("1~");
			BillingVo vo=getBillingInfo();
			System.out.println("2~");
			billingService.addVo(vo);
			System.out.println("3~");
			page="refresh";
		} else if("modify".equals(method)){
			BillingVo modifyList=billingService.findBillingByPK(modifyId);
			HashMap<String, Integer> map = userInfoService.findAllNameAndId();
			ServletActionContext.getRequest().setAttribute("billUserSelect",
					map);
			modifyList.setProCatagoryName(ProCatagoryUtil.getProCatagoryString(modifyList.getProCatagory()));
			this.getSession().setAttribute("modifyList", modifyList);
			this.getSession().setAttribute("billUser", modifyList.getBillUser());
			page="modify";
		} else if("update".equals(method)){
			BillingVo vo=billingService.findBillingByPK(modifyId);
			vo=getModifyInfo(vo);
			billingService.update(vo);
			page="refresh";
		} else if("deliver".equals(method)){
			BillingVo vo=billingService.findBillingByPK(deliverId);
			vo.setState(2);
			billingService.update(vo);
			page="refresh";
		}
		return page;
	}
	
	public BillingVo getModifyInfo(BillingVo vo){
		vo.setBillCatagory(billCatagory);
		vo.setBillDate(billDate);
		vo.setBillMoney(billMoney);
		vo.setBillNum(billNum);
		vo.setBillUser((UserInfoVo)userInfoService.getUserInfo(Integer.parseInt(billUserId)));
		System.out.println(proCatagory);
		System.out.println(proId);
		vo.setLastRecordUserId(this.getSessionUser().getId());
		vo.setProCatagory(Integer.parseInt(proCatagory));
		System.out.println("11");
		vo.setProId(Integer.parseInt(proId));
		System.out.println("22");
		if("1".equals(proCatagory)){
			vo.setProName(((PriceProjectVo)allProjectService.findProjectByCAndId(1, Integer.parseInt(proId))).getName());
		} else if("2".equals(proCatagory)){
			vo.setProName(((DeleProjectVo)allProjectService.findProjectByCAndId(2, Integer.parseInt(proId))).getProName());
		} else if("3".equals(proCatagory)){
			vo.setProName(((ManageProjectVo)allProjectService.findProjectByCAndId(3, Integer.parseInt(proId))).getProName());
		}
		System.out.println("33");
		return vo;
	}
	
	public BillingVo getBillingInfo(){
		BillingVo vo=new BillingVo();
		vo.setBillCatagory(billCatagory);
		vo.setBillDate(billDate);
		vo.setBillMoney(billMoney);
		vo.setBillNum(billNum);
		vo.setState(1);
		vo.setBillUser((UserInfoVo)userInfoService.getUserInfo(Integer.parseInt(billUserId)));
		System.out.println(proCatagory);
		System.out.println(proId);
		vo.setLastRecordUserId(this.getSessionUser().getId());
		vo.setProCatagory(Integer.parseInt(proCatagory));
		System.out.println("11");
		vo.setProId(Integer.parseInt(proId));
		System.out.println("22");
		if("1".equals(proCatagory)){
			vo.setProName(((PriceProjectVo)allProjectService.findProjectByCAndId(1, Integer.parseInt(proId))).getName());
		} else if("2".equals(proCatagory)){
			vo.setProName(((DeleProjectVo)allProjectService.findProjectByCAndId(2, Integer.parseInt(proId))).getProName());
		} else if("3".equals(proCatagory)){
			vo.setProName(((ManageProjectVo)allProjectService.findProjectByCAndId(3, Integer.parseInt(proId))).getProName());
		}
		System.out.println("33");
		return vo;
	}

	public JSONArray getProArr(int catagory) {
		System.out.println("in catagory");
		System.out.println(catagory);
		List<Vo> voList = null;
		JSONArray objArr = new JSONArray();

		if (catagory == 1) {
			voList = priceProjectService.findAllPriceProject();
			for (int i = 0; i < voList.size(); i++) {
				System.out.println(((PriceProjectVo) voList.get(i)).getId()+"aa");
				JSONObject obj = new JSONObject();
				obj.put("id", ((PriceProjectVo) voList.get(i)).getId());
				obj.put("name", ((PriceProjectVo) voList.get(i)).getName());
				objArr.add(obj);
			}
		} else if (catagory == 2) {
			voList=deleProjectService.findAllDeleProject();
			for (int i = 0; i < voList.size(); i++) {
				JSONObject obj = new JSONObject();
				obj.put("id", ((DeleProjectVo) voList.get(i)).getId());
				obj.put("name", ((DeleProjectVo) voList.get(i)).getProName());
				objArr.add(obj);
			}
		} else if (catagory == 3) {
			voList=manageProjectService.findAllManageProject();
			for (int i = 0; i < voList.size(); i++) {
				JSONObject obj = new JSONObject();
				obj.put("id", ((ManageProjectVo) voList.get(i)).getId());
				obj.put("name", ((ManageProjectVo) voList.get(i)).getProName());
				objArr.add(obj);
			}
		}
		System.out.println(objArr.toString()+"~getPROARR");
		return objArr;
	}

	public void setBillingService(BillingService billingService) {
		this.billingService = billingService;
	}

	public BillingService getBillingService() {
		return billingService;
	}

	public void setMethod(String method) {
		this.method = method;
	}

	public String getMethod() {
		return method;
	}

	public void setMultiType(String multiType) {
		this.multiType = multiType;
	}

	public String getMultiType() {
		return multiType;
	}

	public void setMultiDelete(String multiDelete) {
		this.multiDelete = multiDelete;
	}

	public String getMultiDelete() {
		return multiDelete;
	}

	public void setDelStr(String delStr) {
		this.delStr = delStr;
	}

	public String getDelStr() {
		return delStr;
	}

	public void setDeleteId(String deleteId) {
		this.deleteId = deleteId;
	}

	public String getDeleteId() {
		return deleteId;
	}

	public void setId(int id) {
		this.id = id;
	}

	public int getId() {
		return id;
	}

	public void setProName(String proName) {
		this.proName = proName;
	}

	public String getProName() {
		return proName;
	}

	public void setProCatagory(String proCatagory) {
		this.proCatagory = proCatagory;
	}

	public String getProCatagory() {
		return proCatagory;
	}

	public void setProId(String proId) {
		this.proId = proId;
	}

	public String getProId() {
		return proId;
	}

	public void setBillNum(String billNum) {
		this.billNum = billNum;
	}

	public String getBillNum() {
		return billNum;
	}

	public void setBillCatagory(int billCatagory) {
		this.billCatagory = billCatagory;
	}

	public int getBillCatagory() {
		return billCatagory;
	}

	public void setBillMoney(String billMoney) {
		this.billMoney = billMoney;
	}

	public String getBillMoney() {
		return billMoney;
	}

	public void setBillUser(UserInfoVo billUser) {
		this.billUser = billUser;
	}

	public UserInfoVo getBillUser() {
		return billUser;
	}

	public void setBillDate(Date billDate) {
		this.billDate = billDate;
	}

	public Date getBillDate() {
		return billDate;
	}

	public void setLastRecordUserId(int lastRecordUserId) {
		this.lastRecordUserId = lastRecordUserId;
	}

	public int getLastRecordUserId() {
		return lastRecordUserId;
	}

	public void setPriceProjectService(PriceProjectService priceProjectService) {
		this.priceProjectService = priceProjectService;
	}

	public PriceProjectService getPriceProjectService() {
		return priceProjectService;
	}

	public void setServletRequest(HttpServletRequest request) {
		// TODO Auto-generated method stub
		this.request = request;
	}

	public void setServletResponse(HttpServletResponse response) {
		// TODO Auto-generated method stub
		this.response = response;
	}

	public void setUserInfoService(UserInfoService userInfoService) {
		this.userInfoService = userInfoService;
	}

	public UserInfoService getUserInfoService() {
		return userInfoService;
	}

	public void setBillUserId(String billUserId) {
		this.billUserId = billUserId;
	}

	public String getBillUserId() {
		return billUserId;
	}

	public void setAllProjectService(AllProjectService allProjectService) {
		this.allProjectService = allProjectService;
	}

	public AllProjectService getAllProjectService() {
		return allProjectService;
	}

	public void setModifyId(int modifyId) {
		this.modifyId = modifyId;
	}

	public int getModifyId() {
		return modifyId;
	}

	public void setDeliverId(int deliverId) {
		this.deliverId = deliverId;
	}

	public int getDeliverId() {
		return deliverId;
	}

	public void setDeleProjectService(DeleProjectService deleProjectService) {
		this.deleProjectService = deleProjectService;
	}

	public DeleProjectService getDeleProjectService() {
		return deleProjectService;
	}

	public void setManageProjectService(ManageProjectService manageProjectService) {
		this.manageProjectService = manageProjectService;
	}

	public ManageProjectService getManageProjectService() {
		return manageProjectService;
	}

}
